/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
/**
 * @description PoEcoDaoImpl.java
 * @author dongbin
 * @version 
 * @copyright
 */

package com.erp.order.poeco.dao.hibernate;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.redragon.framework.hibernate.DaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.erp.order.poeco.dao.PoEcoDao;

/**
 * @description
 * @date 2020年10月21日
 * @author dongbin
 * 
 */
@Repository
public class PoEcoDaoImpl implements PoEcoDao {
    
    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;

    @Override
    public BigDecimal getPoLineQuantityByAgreement(String poAgreementHeadCode, String materialCode, Integer poLineId) {
        String sql = "select sum(quantity) from po_head h,po_line l where h.po_head_code = l.po_head_code "
                   + "and h.po_agreement_head_code = :poaheadcode and l.material_code = :materialcode and l.po_line_id <> :polineid";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("poaheadcode", poAgreementHeadCode);
        args.put("materialcode", materialCode);
        args.put("polineid", poLineId);
        
        List list = this.daoSupport.selectDataSqlCount(sql, args);
        if(list!=null&&list.size()>0) {
            try {
                return new BigDecimal(String.valueOf(list.get(0)));
            }catch(Exception e) {}
        }
        
        return BigDecimal.ZERO;
    }

    @Override
    public BigDecimal getPoLineAmountByAgreement(String poAgreementHeadCode, String poHeadCode) {
        String sql = "select sum(amount) from po_head h,po_line l where h.po_head_code = l.po_head_code "
                + "and h.po_agreement_head_code = :poaheadcode and h.po_head_code <> :poheadcode";
     
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("poaheadcode", poAgreementHeadCode);
        args.put("poheadcode", poHeadCode);
     
        List list = this.daoSupport.selectDataSqlCount(sql, args);
        if(list!=null&&list.size()>0) {
            try {
                return new BigDecimal(String.valueOf(list.get(0)));
            }catch(Exception e) {}
        }
     
        return BigDecimal.ZERO;
    }

}
